package com;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

public class ForGetPassword {
        private String givenUserName ;
        private String hintQuestion;
        private String hintAnswer;
        private String retrivedPassword;
        
        public String getRetrivedPassword() {
			return retrivedPassword;
		}
		public void setRetrivedPassword(String retrivedPassword) {
			this.retrivedPassword = retrivedPassword;
		}
		public String getGivenUserName() {
			return givenUserName;
		}
		public void setGivenUserName(String givenUserName) {
			this.givenUserName = givenUserName;
		}
		public String getHintQuestion() {
                return hintQuestion;
        }
        public void setHintQuestion(String hintQuestion) {
                this.hintQuestion = hintQuestion;
        }
        public String getHintAnswer() {
                return hintAnswer;
        }
        public void setHintAnswer(String hintAnswer) {
                this.hintAnswer = hintAnswer;
        }
        public String recoverPassword(){
                System.out.println("givenUserName:"+givenUserName);
                System.out.println("hintQuestion:"+hintQuestion);
                System.out.println("hintAnswer:"+hintAnswer);
                
                try{
                        Properties forgetProps = new Properties();
            forgetProps.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/forGetPassword.properties"));
            
            Set keySet = forgetProps.keySet();
                    if(!keySet.contains(givenUserName)){
                                FacesMessage message = new FacesMessage("User Name incorrect");
                                FacesContext.getCurrentInstance().addMessage("User Name incorrect", message);
                                return "forgetPasswordPage";
                                
                        }else if(forgetProps.getProperty(givenUserName)!=null && !forgetProps.getProperty(givenUserName).trim().equals("")){
                                System.out.println("with in Get Hint Question ");
                                String hintArr[] = forgetProps.getProperty(givenUserName).split("\\?");
                                if(hintArr.length>0){
                                        if(hintQuestion!=null && hintQuestion.equalsIgnoreCase(hintArr[0])){
                                                hintQuestion=hintArr[0];
                                                hintAnswer=hintArr[1];
                                                Properties userdetailsProps = new Properties();
                                                userdetailsProps.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"));
                                                FacesMessage message = new FacesMessage(givenUserName+":"+"hintQuestion:"+hintQuestion+"hintAnswer:"+userdetailsProps.getProperty(givenUserName));
                                        FacesContext.getCurrentInstance().addMessage(givenUserName+":"+"hintQuestion:"+hintQuestion+"hintAnswer:"+userdetailsProps.getProperty(givenUserName), message);
                                        this.retrivedPassword=userdetailsProps.getProperty(givenUserName);
                                        
                                        
                                        return "success";
                                        }else{
                                                FacesMessage message = new FacesMessage("Hint Question is wrong for user"+givenUserName);
                                        FacesContext.getCurrentInstance().addMessage("Hint Question is wrong for user"+givenUserName, message);
                                        return "forgetPasswordPage";
                                        }
                                }else{
                                        FacesMessage message = new FacesMessage("Hint Question and Answer is Null"+givenUserName);
                                        FacesContext.getCurrentInstance().addMessage("Hint Question and Answer is Null"+givenUserName, message);
                                        return "forgetPasswordPage";
                                }
                        }
                }catch(Exception e){
                        e.printStackTrace();
                }
                return null;
        }
        public String loginPage(){
                return "loginPage";
        }
        
}